import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Stickman here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Stickman extends Actor
{
    /**
     * Act - do whatever the Stickman wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    private boolean Muerto;
    private int Movimiento;
    private int tiempoDisparo;
    private int tipoDeArma;

    private GreenfootImage Stickman1;
    private GreenfootImage Stickman2;
    private GreenfootImage Stickman3;
    private GreenfootImage Stickman4;
    private GreenfootImage Stickman5;
    private GreenfootImage Stickman6;
    private GreenfootImage Stickman7;
    private GreenfootImage Stickman8;       

    public Stickman(){

        Movimiento=0;
        tiempoDisparo=0;
        cargaImagenes();
        Muerto=false;
        tipoDeArma=1;

    }

    public void act() 
    {
        if(Muerto!=true){
            MovimientoTeclas();
            tiempoDisparo--;
            Dano();
        }else{
            if(getY()>66){setLocation (getX(), getY() - 1);}else{Greenfoot.stop();}        
        }
    }   

    public void nada(int direccion){

        Movimiento++;
        if(direccion==1){
            if(Movimiento==1){ setImage(Stickman1);}
            if(Movimiento==10){ setImage(Stickman2);}
            if(Movimiento==20){ setImage(Stickman3);}
            if(Movimiento==30){ setImage(Stickman4);}
            if(Movimiento==40){ setImage(Stickman5);}
            if(Movimiento==50){ setImage(Stickman6);}
            if(Movimiento==60){ setImage(Stickman7);}
            if(Movimiento==70){ setImage(Stickman8);}  
            if(Movimiento==80){Movimiento=0;}
        }
        if(direccion==2){
            if(Movimiento==1){setImage(Stickman8);}
            if(Movimiento==10){setImage(Stickman7);}
            if(Movimiento==20){setImage(Stickman6);}
            if(Movimiento==30){setImage(Stickman5);}
            if(Movimiento==40){setImage(Stickman4);}
            if(Movimiento==50){setImage(Stickman3);}
            if(Movimiento==60){setImage(Stickman2);}
            if(Movimiento==70){setImage(Stickman1);}  
            if(Movimiento==80){Movimiento=0;}
        }
    }

    public void cargaImagenes(){

        Stickman1 = new GreenfootImage("Stickman 1.png");
        Stickman2 = new GreenfootImage("Stickman 2.png");
        Stickman3 = new GreenfootImage("Stickman 3.png");
        Stickman4 = new GreenfootImage("Stickman 4.png");
        Stickman5 = new GreenfootImage("Stickman 5.png");
        Stickman6 = new GreenfootImage("Stickman 6.png");
        Stickman7 = new GreenfootImage("Stickman 7.png");
        Stickman8 = new GreenfootImage("Stickman 8.png");   
        setImage(Stickman1);

    }

    public void MovimientoTeclas(){

        // Add your action code here.

        if(Greenfoot.isKeyDown("right")){this.nada(1);if(this.getWorld().getWidth()-10>getX() &&  intersecciones()==true){setLocation(getX()+2, getY());}}
        if(Greenfoot.isKeyDown("left")){this.nada(2);if(this.getX()>10 ){setLocation(getX()-2, getY());}}
        if (Greenfoot.isKeyDown("up") ) {  
            this.nada(1);
            if(getY()>67 && intersecciones()==true){setLocation (getX(), getY() - 2);}
        }else
        {      
            if(getY()<380 ){setLocation (getX(), getY() + 1);}           
        }
         if (Greenfoot.isKeyDown("f") && (tiempoDisparo <= 0)){
          
             getWorld().addObject(new Arma(0,tipoDeArma), getX()+20, getY()-10);//crea una arma (giro,tipo)
           
            //Greenfoot.playSound("shot1.wav");
            tiempoDisparo = 25;      
        }
        if (Greenfoot.isKeyDown("space") && (tiempoDisparo <= 0)){
          
             getWorld().addObject(new Arma(0,3), getX(), getY());//crea una arma (giro,tipo)//crea bomba
           
            //Greenfoot.playSound("shot1.wav");
            tiempoDisparo = 25;      
        }
        if(getY()==380){this.nada(1);}          
    }

    public void Dano(){

        Actor aux1 = getOneIntersectingObject(Pulpos.class);
        if (aux1 != null)
        {
            World aux=getWorld();
            ((StickmanWorld)aux).ReduceVida(2);
            getWorld().addObject(new Animaciones(4),getX(),getY());
            getWorld().addObject(new Animaciones(5),(getWorld().getWidth()/2),(getWorld().getHeight()/2));
            getWorld().removeObject(aux1);
            //Muerto=true;
        }
        Actor aux3 = getOneIntersectingObject(Piranas.class);
        if (aux3 != null)
        {
            World aux=getWorld();
           ((StickmanWorld)aux).ReduceVida(2);
            getWorld().addObject(new Animaciones(4),getX(),getY());
            getWorld().addObject(new Animaciones(5),(getWorld().getWidth()/2),(getWorld().getHeight()/2));
            getWorld().removeObject(aux3);
        }      
        Actor burbujas = getOneIntersectingObject(Burbujas.class);
        if (burbujas!= null)
        {
            World aux=getWorld();
            ((StickmanWorld)aux).AumentaBurbujas();           
            getWorld().removeObject(burbujas);
        }
        Actor medusas = getOneIntersectingObject(Medusas.class);
        if (medusas!= null)
        {
            World aux=getWorld();
           ((StickmanWorld)aux).ReduceVida(2);
            getWorld().addObject(new Animaciones(4),getX(),getY());
            getWorld().addObject(new Animaciones(5),(getWorld().getWidth()/2),(getWorld().getHeight()/2));
            getWorld().removeObject(medusas);
            
        }
    }

    public boolean intersecciones(){        
        Actor barco=getOneIntersectingObject(Barco.class);
        Actor ballena=getOneIntersectingObject(Ballenas.class);
        if(barco!=null || ballena!=null){return false;}else{return true;}
    }
    public void cambiaArma(int nuevaArma){
    tipoDeArma=nuevaArma;
    }

}
